import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { AudioManager } from "../manager/AudioManager";
import { EventManager } from "../manager/EventManager";
import { LevelManager } from "../manager/LevelManager";
import { ResManager } from "../manager/ResManager";
import { UIManager } from "../manager/UIManager";
import { LvInfo } from "../model/OtherModel";

const { ccclass, property } = cc._decorator;

@ccclass
export class RecommendUI extends cc.Component {
    @property(cc.Label)
    levelName: cc.Label = null;
    @property(cc.Sprite)
    levelSpr: cc.Sprite = null;

    lvInfo: LvInfo = null;
    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
        AdManager.naoLiReportVideo(1, "立即体验推荐关卡");
    }
    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this)
    }

    async openUI(lvInfo: LvInfo) {
        this.lvInfo = lvInfo;
        this.levelName.string = this.lvInfo.name;
        let sprName = `game_${this.lvInfo.type}_${this.lvInfo.lv}`;
        let spr = await ResManager.ins.loadRes(sprName, cc.SpriteFrame, BUNDLE_TYPE_ENUM.LEVEL_SPR)
        this.levelSpr.spriteFrame = spr;
    }
    clickBtnClose() {
        AudioManager.playClick();
        UIManager.ins.close();
    }

    /** 立即体验 */
    clickBtnGoto() {
        AudioManager.playClick();
        AdManager.naoLiReportVideo(2, "立即体验推荐关卡");
        AdManager.reportVideo(3, -1, "推荐关卡");
        AdManager.showVideoAd(() => {
            AdManager.reportOverVideo(-1, "推荐关卡");
            AdManager.naoLiReportVideo(3, "立即体验推荐关卡");
            AdManager.naoLiReportVideo(4, "立即体验推荐关卡");
            LevelManager.ins.curLvInfo = this.lvInfo;
            LevelManager.ins.unlockCurrentLevelTemp();
            EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this)
            LevelManager.ins.loadLevelPre();
        });
    }
}